package com.app.newiptv.helper;

import com.app.newiptv.base.MyApplication;
import com.app.newiptv.bean.RequestDto;
import com.app.newiptv.bean.allchannels.MainChanel;
import com.app.newiptv.bean.allchannels.Program;
import com.app.newiptv.callback.IRequestLisenter;
import com.app.newiptv.callback.RequestCallback;
import com.app.newiptv.utils.CacheDataPresenter;
import com.app.newiptv.utils.OkGoUtil;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by Shinelon on 2017/9/15.
 */

public class AllChannelsHelper {
    private RequestCallback callback;
    private List<MainChanel> mainChanels;

    public AllChannelsHelper() {
        mainChanels = new ArrayList<>();
    }

    public void setCallback(RequestCallback callback) {
        this.callback = callback;
    }

    public void loadData(String url) {
        OkGoUtil<MainChanel> okGoUtil = new OkGoUtil<MainChanel>(MainChanel.class, OkGoUtil.TYPE_ARRAY, new IRequestLisenter() {
            @Override
            public void onSuccessData(RequestDto dto) {
                mainChanels = (List<MainChanel>) dto.getList();
                if (callback != null) {
                    List<Program> programList = CacheDataPresenter.getPresenter(MyApplication.mApp).loadProgramAll();
                    if (programList != null && programList.size() > 0) {
                        MainChanel mainChanel = new MainChanel();
                        mainChanel.setId(MyApplication.COLLECTIION);
                        mainChanel.setName("我的收藏");
                        mainChanels.add(mainChanel);
                    }
                    callback.onLoadComplete("", 0, mainChanels.size(), mainChanels);
                }
            }

            @Override
            public void onErrer(int code, String errMsg) {
            }
        });
        okGoUtil.requestGet(url);
    }
}
